﻿/*
* The shader code below is licensed under the Creative Commons Attribution-Share Alike 3.0 
* license.
*
* You are free:
*	To Share: to copy, distribute, display, and perform the work
*	To Remix: to make derivative works 
*
*	Under the following conditions:
*		* Attribution - You must attribute the work in the manner specified by the author or
*			licensor (but not in any way that suggests that they endorse you or your use of the work).
*		* Share Alike - If you alter, transform, or build upon this work, you may distribute
*			the resulting work only under the same, similar or a compatible license.
*
*		For any reuse or distribution, you must make clear to others the license terms of this work.
*		The best way to do this is with a link to the above web page (http;//www.tesla-engine.net)
*
*		Any of the above conditions can be waived if you get permission from the copyright holder.
*
*		Apart from the remix rights granted under this license, nothing in this license impairs or
*		restricts the author's moral rights.
*/

//World params
uniform float3 CamPos;
uniform float4x4 View;
uniform float4x4 Proj;

uniform textureCUBE DiffuseMap;
uniform samplerCUBE DiffuseMapSampler =
sampler_state {
  Texture = <DiffuseMap>;
  MipFilter = LINEAR;
  MinFilter = LINEAR;
  MagFilter = LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
};

struct VSOutput {
	float4 PositionPS : SV_Position;
	float3 TexCoord : TEXCOORD;
};


VSOutput VS_Skybox(float3 Position : POSITION) {
	float4x4 world = { 1, 0, 0, CamPos.x,
					   0, 1, 0, CamPos.y,
					   0, 0, 1, CamPos.z,
					   0, 0, 0, 1 };

	VSOutput output;

	//Rotate the position via the view matrix, then translate it
	//via the camera position so the cube is always centered to the camera
	float3 posWS = mul(mul(Position, View), world);

	output.PositionPS = mul(float4(posWS, 1.0f), Proj).xyww;
	output.TexCoord = Position;

	return output;
}

float4 PS_Skybox(VSOutput input) : SV_Target {
	return texCUBE(DiffuseMapSampler, input.TexCoord);
}

technique Skybox {
	pass Pass0 {
    VertexShader = compile vs_2_0 VS_Skybox();
    PixelShader = compile ps_2_0 PS_Skybox();
	}
}